package com.redrose.videodemo.camera;

import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.view.SurfaceHolder;

import com.redrose.videodemo.utils.LogUtils;

import java.io.IOException;
import java.util.List;
/**
 * Desc: Camera 工具类
 * @author: RedRose
 * Date: 2019/5/3
 * Email: yinsxi@163.com
 */
public class CameraUtil2 {
    public static final String TAG = "CameraUtil2";
    private static CameraUtil2 mInstance;
    private static boolean isPreviewing = false;
    /**
     * 相机参数对象
     */
    private Camera.Parameters mParams;
    private Camera.Parameters mParameters;
    /**
     * 闪光灯自动
     */
    public static final int FLASH_AUTO = 0;
    /**
     * 闪光灯关闭
     */
    public static final int FLASH_OFF = 1;
    /**
     * 闪光灯开启
     */
    public static final int FLASH_ON = 2;

    private CameraUtil2() {
    }

    private static final Object o = new Object();

    public static CameraUtil2 getInstance() {
        if (mInstance == null) {
            synchronized (o) {
                if (mInstance == null) {
                    mInstance = new CameraUtil2();
                }
            }
        }
        return mInstance;
    }

    private Camera mCamera;

    public void doStartPreview(SurfaceTexture surface) {
        LogUtils.i(TAG, "doStartPreview...");
        if (isPreviewing) {
            mCamera.stopPreview();
            return;
        }
        if (mCamera != null) {
            try {
                mCamera.setPreviewTexture(surface);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            setProperty();
        }

    }

    public void doStartPreview(SurfaceHolder holder) {
        LogUtils.i(TAG, "doStartPreview...");
        if (isPreviewing) {
            mCamera.stopPreview();
            return;
        }
        if (mCamera != null) {
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            setProperty();
        }


    }
    public Camera openCamera() {
        // 0 表示开启后置相机
        return openCamera(0);
    }

    private Camera openCamera(int id) {
        if (mCamera == null) {
            mCamera = Camera.open(id);
        }
//        setProperty();
        return mCamera;
    }

    /**
     * 相机属性设置
     */
    private void setProperty() {
        //设置相机预览页面旋转90°，（默认是横屏）
        mCamera.setDisplayOrientation(90);
        mParameters = mCamera.getParameters();
        //设置将保存的图片旋转90°（竖着拍摄的时候）
        mParameters.setRotation(90);
        mParameters.setPictureFormat(ImageFormat.NV21);
        mParameters.setPreviewFormat(ImageFormat.NV21);
        mParameters.setPreviewSize(1920, 1080);
        mParameters.setPictureSize(1920, 1080);
//        mParameters.setPictureSize(4608, 3456);
        mParameters.setPictureFormat(ImageFormat.JPEG);
        mParameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
//        mParameters.set(ImageFormat.YUV_444_888);
        mCamera.setParameters(mParameters);
        mCamera.startPreview();
        isRelease = false;
        isPreviewing = true;
    }

    /**
     * 选装图片的角度
     */
    public void setRotateDegree(int degree) {
//        boolean result = degree == 0 || degree == 90
//        mParameters.setRotation(90);
        if (mCamera != null) {
            mParameters = mCamera.getParameters();
            mParameters.setRotation(degree);
            mCamera.setParameters(mParameters);
        }

    }
    public boolean getIsPreview(){
        return isPreviewing;
    }
    /**
     * 获取支持的预览分辨率
     */
    public List<Camera.Size> getPreviewSizeList() {
        if (mCamera == null) {
            throw new NullPointerException("Camera can not be null");
        }
        return mCamera.getParameters().getSupportedPreviewSizes();
    }

    /**
     * 获取保存图片支持的分辨率
     */
    public List<Camera.Size> getPictureSizeList() {
        if (mCamera == null) {
            throw new NullPointerException("Camera can not be null");
        }
        return mCamera.getParameters().getSupportedPictureSizes();
    }

    /**
     * 设置闪光灯模式
     */
    public void setFlashMode(int mode) {
        if (mCamera == null) {
            return;
        }
        mParameters = mCamera.getParameters();
        String flashMode = mParameters.getFlashMode();
        switch (mode) {
            case FLASH_AUTO:
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
                break;
            case FLASH_OFF:
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                break;
            case FLASH_ON:
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
                break;
            default:
                break;
        }
        mCamera.setParameters(mParameters);
    }

    /**
     * 释放相机资源
     */
    public void releaseCamera() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.lock();
            mCamera.release();
            mCamera = null;
            isRelease = true;
            isPreviewing = false;
        }
    }

    /**
     * 是否旋转图片 true 选装
     */
    private boolean isRelease = false;

    public boolean getIsRelease() {
        return isRelease;
    }

    /**
     * 设置保存图片的分辨率
     */
    public void setSaveSize(Camera.Size saveSize) {
        mParameters.setPictureSize(saveSize.width, saveSize.height);
        mCamera.setParameters(mParameters);
    }
}
